﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using XML;
using FileSystem;
using Search;
using Windows;

namespace PureUSB
{
    public partial class frmmain : Form
    {
        // Load Classes
        XML.XML tehXML = new XML.XML();
        FileSystem.Dir Dir = new FileSystem.Dir();
        Search.Array Search = new Search.Array();
        Windows.App App = new Windows.App();
        Windows.PC PC = new Windows.PC();

        // ListMenu
        public string[] listItems;

        // Search Engine
        public bool isDefaultSearch = true;
        public bool firstClick = true;
        public string[] Results;

        // PureUSB Settings
        public string windir = Environment.GetEnvironmentVariable("WINDIR");
        public string DocumentPath, PicturePath, MusicPath, VideoPath;
        public string AppDrive;
        public int AppExtractType; // 1 = USB Drive, 2 = Main Drive, 3 = Drive with most space.
        public string ThemeName;


        public frmmain()
        {
            InitializeComponent();
        }

        // Form Events
        private void frmmain_Load(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            this.TopMost = true;
            this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
            this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
            this.Focus();
            Load_Settings();
        }
        private void frmmain_Deactivate(object sender, EventArgs e)
        {
            this.Hide();
        }

        public string Drive_Path()
        {
            string path = Application.StartupPath;
            string[] folders = path.Split('\\');
            return folders[0] + "\\";
        }
        public string Get_Drive(string path)
        {
            string[] folders = path.Split('\\');
            return folders[0] + "\\";
        }

        private void Load_Settings()
        {
            DriveInfo lc = new DriveInfo(Get_Drive(windir));
            DriveInfo usb = new DriveInfo(Drive_Path());

            if (File.Exists(Application.StartupPath + "\\Settings.xml") == true)
            {
                tehXML.Load_XML(Application.StartupPath + "\\Settings.xml", "Settings/");
                DocumentPath = tehXML.GetSetting("Folders/Documents", Drive_Path() + "Media\\Documents");
                PicturePath = tehXML.GetSetting("Folders/Pictures", Drive_Path() + "Media\\Pictures");
                MusicPath = tehXML.GetSetting("Folders/Music", Drive_Path() + "Media\\Music");
                VideoPath = tehXML.GetSetting("Folders/Videos", Drive_Path() + "Media\\Videos");
                ThemeName = tehXML.GetSetting("Theme/Name", "Blue");
                AppExtractType = tehXML.GetSetting("Applications/AppExtractType", 3);
            }
            else
            {
                tehXML.Load_XML(Application.StartupPath + "\\Settings.xml", "Settings/");
                tehXML.SetSetting("Folders/Documents", Drive_Path() + "Media\\Documents");
                tehXML.SetSetting("Folders/Pictures", Drive_Path() + "Media\\Pictures");
                tehXML.SetSetting("Folders/Music", Drive_Path() + "Media\\Music");
                tehXML.SetSetting("Folders/Videos", Drive_Path() + "Media\\Videos");
                tehXML.SetSetting("Theme/Name", "Blue");
                tehXML.SetSetting("Applications/AppExtractType", 3);
                Load_Settings();
            }

            // Load Context Menus
            if (lc.AvailableFreeSpace > usb.AvailableFreeSpace)
                AppDrive = Get_Drive(windir);
            else
                AppDrive = Drive_Path();
            localdriveToolStripMenuItem.Text = "Local Drive (" + Get_Drive(windir) + ")";
            usbdriveToolStripMenuItem.Text = "USB Drive (" + Drive_Path() + ")";
            freespacedriveToolStripMenuItem.Text = "Larger Drive (" + AppDrive + ")";

            if (AppExtractType == 1)
                localdriveToolStripMenuItem.CheckState = CheckState.Checked;
            else if (AppExtractType == 2)
                usbdriveToolStripMenuItem.CheckState = CheckState.Checked;
            else
                freespacedriveToolStripMenuItem.CheckState = CheckState.Checked;

            // Load GUI
            pBoxDoc.Image = PureUSB.Properties.Resources.MyDocumentsDefault;
            pBoxPic.Image = PureUSB.Properties.Resources.MyPicturesDefault;
            pBoxMusic.Image = PureUSB.Properties.Resources.MyMusicDefault;
            pBoxVid.Image = PureUSB.Properties.Resources.MyVideosDefault;

            if (ThemeName == "Blue")
            {
                pBoxWaterMark.Image = PureUSB.Properties.Resources.Blue_Tree;
                this.BackgroundImage = PureUSB.Properties.Resources.Blue_BackGround;
            }
            else if (ThemeName == "Green")
            {
                pBoxWaterMark.Image = PureUSB.Properties.Resources.Green_Tree;
                this.BackgroundImage = PureUSB.Properties.Resources.Green_BackGround;
            }
            else if (ThemeName == "Red")
            {
                pBoxWaterMark.Image = PureUSB.Properties.Resources.Red_Tree;
                this.BackgroundImage = PureUSB.Properties.Resources.Red_Background;
            }
            else
            {
                pBoxWaterMark.Image = PureUSB.Properties.Resources.Blue_Tree;
                this.BackgroundImage = PureUSB.Properties.Resources.Blue_BackGround;
            }
        }

        // Search Events
        private void txtSearch_Enter(object sender, EventArgs e)
        {
            if (isDefaultSearch == true)
            {
                txtSearch.Text = "";
                txtSearch.ForeColor = System.Drawing.Color.Black;
                isDefaultSearch = false;
            }
            else
            {
                txtSearch.SelectionStart = 0;
                txtSearch.SelectionLength = txtSearch.Text.Length;
            }
        }
        private void txtSearch_Leave(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                txtSearch.Text = "Search";
                isDefaultSearch = true;
                txtSearch.ForeColor = System.Drawing.Color.Gray;
            }
            firstClick = true;
        }
        private void txtSearch_Click(object sender, EventArgs e)
        {
            if (firstClick == true)
            {
                if (txtSearch.SelectedText == "")
                    txtSearch.SelectAll();
                firstClick = false;
            }
        }
        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                if (txtSearch.Text == "exit")
                    Application.Exit();
                App.New(txtSearch.Text, null);
                txtSearch.Text = "";
                isDefaultSearch = true;
                firstClick = true;
                txtSearch.AutoCompleteCustomSource.Add(txtSearch.Text);
                e.Handled = true;
            }
            else if (e.KeyChar == Convert.ToChar(Keys.Up))
            {

            }
        }


        // Tray Events
        private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }
        private void trayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                rTrayMenu.Show();
                rTrayMenu.Left = MousePosition.X;
                rTrayMenu.Top = MousePosition.Y;
            }
            else
            {
                lTrayMenu.Show();
                lTrayMenu.Left = MousePosition.X;
                lTrayMenu.Top = MousePosition.Y;
            }
        }

        // Right Click Menu
        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void About_Click(object sender, EventArgs e)
        {
            frmabout frm = new frmabout();
            frm.Show();
        }

        // Left Click Menu
        private void localdriveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            localdriveToolStripMenuItem.CheckState = CheckState.Checked;
            usbdriveToolStripMenuItem.CheckState = CheckState.Unchecked;
            freespacedriveToolStripMenuItem.CheckState = CheckState.Unchecked;

            AppExtractType = 1;
            tehXML.SetSetting("Applications/AppExtractType", 1);
        }
        private void usbdriveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            localdriveToolStripMenuItem.CheckState = CheckState.Unchecked;
            usbdriveToolStripMenuItem.CheckState = CheckState.Checked;
            freespacedriveToolStripMenuItem.CheckState = CheckState.Unchecked;

            AppExtractType = 2;
            tehXML.SetSetting("Applications/AppExtractType", 2);
        }
        private void freespacedriveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            localdriveToolStripMenuItem.CheckState = CheckState.Unchecked;
            usbdriveToolStripMenuItem.CheckState = CheckState.Unchecked;
            freespacedriveToolStripMenuItem.CheckState = CheckState.Checked;

            AppExtractType = 3;
            tehXML.SetSetting("Applications/AppExtractType", 3);
        }

        // GUI_Buttons
        private void pBoxDoc_MouseLeave(object sender, EventArgs e)
        {
            pBoxDoc.Image = PureUSB.Properties.Resources.MyDocumentsDefault;
        }
        private void pBoxDoc_MouseMove(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxDoc.Image = PureUSB.Properties.Resources.BDD;
                else
                    pBoxDoc.Image = PureUSB.Properties.Resources.BDO;
            }
            else if (ThemeName == "Green")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxDoc.Image = PureUSB.Properties.Resources.GDD;
                else
                    pBoxDoc.Image = PureUSB.Properties.Resources.GDO;
            }
            else if (ThemeName == "Red")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxDoc.Image = PureUSB.Properties.Resources.RDD;
                else
                    pBoxDoc.Image = PureUSB.Properties.Resources.RDO;
            }
            else
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxDoc.Image = PureUSB.Properties.Resources.BDD;
                else
                    pBoxDoc.Image = PureUSB.Properties.Resources.BDO;
            }

        }
        private void pBoxDoc_MouseDown(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
                pBoxDoc.Image = PureUSB.Properties.Resources.BDD;
            else if (ThemeName == "Green")
                pBoxDoc.Image = PureUSB.Properties.Resources.GDD;
            else if (ThemeName == "Red")
                pBoxDoc.Image = PureUSB.Properties.Resources.RDD;
            else
                pBoxDoc.Image = PureUSB.Properties.Resources.BDD;
        }
        private void pBoxDoc_MouseUp(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
                pBoxDoc.Image = PureUSB.Properties.Resources.BDO;
            else if (ThemeName == "Green")
                pBoxDoc.Image = PureUSB.Properties.Resources.GDO;
            else if (ThemeName == "Red")
                pBoxDoc.Image = PureUSB.Properties.Resources.RDO;
            else
                pBoxDoc.Image = PureUSB.Properties.Resources.BDO;
        }

        private void pBoxPic_MouseLeave(object sender, EventArgs e)
        {
            pBoxPic.Image = PureUSB.Properties.Resources.MyPicturesDefault;
        }
        private void pBoxPic_MouseMove(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxPic.Image = PureUSB.Properties.Resources.BPD;
                else
                    pBoxPic.Image = PureUSB.Properties.Resources.BPO;
            }
            else if (ThemeName == "Green")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxPic.Image = PureUSB.Properties.Resources.GPD;
                else
                    pBoxPic.Image = PureUSB.Properties.Resources.GPO;
            }
            else if (ThemeName == "Red")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxPic.Image = PureUSB.Properties.Resources.RPD;
                else
                    pBoxPic.Image = PureUSB.Properties.Resources.RPO;
            }
            else
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxPic.Image = PureUSB.Properties.Resources.BPD;
                else
                    pBoxPic.Image = PureUSB.Properties.Resources.BPO;
            }
        }
        private void pBoxPic_MouseDown(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
                pBoxPic.Image = PureUSB.Properties.Resources.BPD;
            else if (ThemeName == "Green")
                pBoxPic.Image = PureUSB.Properties.Resources.GPD;
            else if (ThemeName == "Red")
                pBoxPic.Image = PureUSB.Properties.Resources.RPD;
            else
                pBoxPic.Image = PureUSB.Properties.Resources.BPD;
        }
        private void pBoxPic_MouseUp(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
                pBoxPic.Image = PureUSB.Properties.Resources.BPO;
            else if (ThemeName == "Green")
                pBoxPic.Image = PureUSB.Properties.Resources.GPO;
            else if (ThemeName == "Red")
                pBoxPic.Image = PureUSB.Properties.Resources.RPO;
            else
                pBoxPic.Image = PureUSB.Properties.Resources.BPO;
        }

        private void pBoxMusic_MouseLeave(object sender, EventArgs e)
        {
            pBoxMusic.Image = PureUSB.Properties.Resources.MyMusicDefault;
        }
        private void pBoxMusic_MouseMove(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxMusic.Image = PureUSB.Properties.Resources.BMD;
                else
                    pBoxMusic.Image = PureUSB.Properties.Resources.BMO;
            }
            else if (ThemeName == "Green")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxMusic.Image = PureUSB.Properties.Resources.GMD;
                else
                    pBoxMusic.Image = PureUSB.Properties.Resources.GMO;
            }
            else if (ThemeName == "Red")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxMusic.Image = PureUSB.Properties.Resources.RMD;
                else
                    pBoxMusic.Image = PureUSB.Properties.Resources.RMO;
            }
            else
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxMusic.Image = PureUSB.Properties.Resources.BMD;
                else
                    pBoxMusic.Image = PureUSB.Properties.Resources.BMO;
            }
        }
        private void pBoxMusic_MouseDown(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
                pBoxMusic.Image = PureUSB.Properties.Resources.BMD;
            else if (ThemeName == "Green")
                pBoxMusic.Image = PureUSB.Properties.Resources.GMD;
            else if (ThemeName == "Red")
                pBoxMusic.Image = PureUSB.Properties.Resources.RMD;
            else
                pBoxMusic.Image = PureUSB.Properties.Resources.BMD;
        }
        private void pBoxMusic_MouseUp(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
                pBoxMusic.Image = PureUSB.Properties.Resources.BMO;
            else if (ThemeName == "Green")
                pBoxMusic.Image = PureUSB.Properties.Resources.GMO;
            else if (ThemeName == "Red")
                pBoxMusic.Image = PureUSB.Properties.Resources.RMO;
            else
                pBoxMusic.Image = PureUSB.Properties.Resources.BMO;
        }

        private void pBoxVid_MouseLeave(object sender, EventArgs e)
        {
            pBoxVid.Image = PureUSB.Properties.Resources.MyVideosDefault;
        }
        private void pBoxVid_MouseMove(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxVid.Image = PureUSB.Properties.Resources.BVD;
                else
                    pBoxVid.Image = PureUSB.Properties.Resources.BVO;
            }
            else if (ThemeName == "Green")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxVid.Image = PureUSB.Properties.Resources.GVD;
                else
                    pBoxVid.Image = PureUSB.Properties.Resources.GVO;
            }
            else if (ThemeName == "Red")
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxVid.Image = PureUSB.Properties.Resources.RVD;
                else
                    pBoxVid.Image = PureUSB.Properties.Resources.RVO;
            }
            else
            {
                if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
                    pBoxVid.Image = PureUSB.Properties.Resources.BVD;
                else
                    pBoxVid.Image = PureUSB.Properties.Resources.BVO;
            }
        }
        private void pBoxVid_MouseDown(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
                pBoxVid.Image = PureUSB.Properties.Resources.BVD;
            else if (ThemeName == "Green")
                pBoxVid.Image = PureUSB.Properties.Resources.GVD;
            else if (ThemeName == "Red")
                pBoxVid.Image = PureUSB.Properties.Resources.RVD;
            else
                pBoxVid.Image = PureUSB.Properties.Resources.BVD;
        }
        private void pBoxVid_MouseUp(object sender, MouseEventArgs e)
        {
            if (ThemeName == "Blue")
                pBoxVid.Image = PureUSB.Properties.Resources.BVO;
            else if (ThemeName == "Green")
                pBoxVid.Image = PureUSB.Properties.Resources.GVO;
            else if (ThemeName == "Red")
                pBoxVid.Image = PureUSB.Properties.Resources.RVO;
            else
                pBoxVid.Image = PureUSB.Properties.Resources.BVO;
        }

        private void pBoxDoc_Click(object sender, EventArgs e)
        {
            //App.New(windir + "\\explorer.exe", DocumentPath);
        }
        private void pBoxPic_Click(object sender, EventArgs e)
        {
            //App.New(windir + "\\explorer.exe", PicturePath);
        }
        private void pBoxMusic_Click(object sender, EventArgs e)
        {
            //App.New(windir + "\\explorer.exe", MusicPath);
        }
        private void pBoxVid_Click(object sender, EventArgs e)
        {
            //App.New(windir + "\\explorer.exe", VideoPath);
        }
    }
}
